//
//  SERegistrationChooseNumController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/6.
//

import UIKit

class SERegistrationChooseNumController: SEBaseViewController {

    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.register(UINib(nibName: SERegistrationChooseNumCell.className,
                                          bundle: nil),
                                    forCellWithReuseIdentifier: SERegistrationChooseNumCell.reuseId)
        }
    }

    let doctorShift: SERegistrationDoctorShift
    var viewModel: SERegistrationChooseNumViewModel? {
        didSet {
            collectionView.reloadData()
        }
    }
    var numItem = SERegistrationNumItem()

    init(with doctorShift: SERegistrationDoctorShift) {
        self.doctorShift = doctorShift
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        dateLabel.text = "\(doctorShift.shiftDate)  \(doctorShift.registerPeriod)"
        requestNumList()
    }

    func pushRegistrationConfirmViewController() {
        guard let vm = viewModel else { return }
        let vc = SERegistrationConfirmController(with: numItem, doctorShift: doctorShift, needBindCard: vm.model.needBindCard, hospitalId: vm.model.hospitalId, hospitalName: vm.model.hospitalName)
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension SERegistrationChooseNumController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        guard let vm = viewModel else {
            return 0
        }
        return vm.numberOfItems
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SERegistrationChooseNumCell.reuseId,
                                                      for: indexPath) as! SERegistrationChooseNumCell
        if let vm = viewModel {
            cell.updateCell(model: vm.numItem(indexPath: indexPath))
        }
        return cell
    }
}

extension SERegistrationChooseNumController: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (UIScreen.screenWidth - 50) / 3.0, height: 66)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let vm = viewModel else {
            return
        }
        collectionView.isUserInteractionEnabled = false
        numItem = vm.numItem(indexPath: indexPath)
        requestNumFee()
    }
}

// MARK: - APIRequest

extension SERegistrationChooseNumController {
    @objc func requestNumList() {
        SEHttpManager.shared.request(SERegistrationAPI.registrationNumList(id: doctorShift.id), model: SERegistrationChooseNumModel.self, withLoading: false) { [weak self] (model, status) in
            switch status {
            case .success:
                if let model = model {
                    self?.viewModel = SERegistrationChooseNumViewModel(model: model)
                }
            case .auth(let msg):
                self?.presentLoginViewController(selector: #selector(self?.requestNumList)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    @objc func requestNumFee() {
        SEHttpManager.shared.requestForAny(SERegistrationAPI.registrationNumFee(id: numItem.id), withLoading: true) { [weak self] (fee, status) in
            self?.collectionView.isUserInteractionEnabled = true
            switch status {
            case .success:
                if let fee = fee {
                    self?.numItem.fee = fee as! String
                    self?.pushRegistrationConfirmViewController()
                } else {
                    SEHUD.showError(message: "号源价格查询错误！")
                }
            case .auth(let msg):
                self?.presentLoginViewController(selector: #selector(self?.requestNumFee)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

}
